package com.ykg.service.impl;

import com.ykg.dto.CUserDTO;
import com.ykg.mapper.CompanyUserMapper;
import com.ykg.pojo.CompanyUser;
import com.ykg.service.ICuserService;
import com.ykg.util.CacheUtil;
import com.ykg.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * TODO
 * 类(接口)：
 * Created on 15:37 2021/6/23.
 *
 * @Author: laobian
 * @Description:
 */
@Service
public class CuserServiceImpl implements ICuserService {
    @Autowired
    JwtUtil jwtUtil;
    @Autowired
    private CompanyUserMapper companyUserMapper;
    @Override
    /**
    * TODO
    *  公司用户登录
    * @params：
     @param cuserName
    	 * @param password
    * @return {@link String}
    * @Author: laobian
    * @Date: 15:50 2021/6/23
    */
    public CUserDTO login(String cuserName, String password) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("account",cuserName);
        map.put("password",password);
        CompanyUser companyUser = companyUserMapper.findByAccount(cuserName,password);
        if(Objects.isNull(companyUser)){
            return  null;
        }
        Long cid = companyUser.getCuserId();
        int type = companyUser.getIdenfity();
        companyUser.setPassword(null);
        String userId = "c"+cid.toString();
        String token  = jwtUtil.generateToken(userId);
        CacheUtil.put(userId,token);
        CUserDTO CUserDTO = new CUserDTO(type,token,companyUser);
        return CUserDTO;
    }
}

